﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModEmployee
{
    public partial class EmpStatistic : System.Web.UI.Page
    {
        string stateID = "1";
        EmployeeBiz employeeBiz = new EmployeeBiz();
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (!IsPostBack)
            {                
                DefDicBiz defDicBiz = new DefDicBiz();
                defDicBiz.InitDropDownList("Admin_Work_State", ddlStateID, "");
                ddlStateID.Items.Add(new ListItem("在职", "10000"));
                defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlDutyID, "");
                defDicBiz.InitDropDownList("Admin_Work_State", ddlStateID0, "");
                ddlStateID0.Items.Add(new ListItem("在职", "10000"));
                new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
            }
        }

        private void BindChart(int r,DataTable dt,string yValue,string xValue)
        {
            
            chMain.ChartAreas["ca"].AxisX.Interval = 1;
            if (r <= 6)
            {
                chMain.Height = r * 50;
                chMain.Width = r * 50;
                chMain.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
            }
            else
            {
                chMain.Height = 280;
                chMain.Width = r * 40;
            }
            chMain.IsSoftShadows = true;
            chMain.DataSource = dt;
            chMain.Series[0].YValueMembers = yValue;
            chMain.Series[0].XValueMember = xValue;
            chMain.DataBind();
        }
        protected void btnByDept_Click(object sender, EventArgs e)
        {
            stateID = ddlStateID.SelectedValue;
            string dutyID = ddlDutyID.SelectedValue;
            string caption = "<b>按部门统计人数</b><br />";

            gvList.Caption = caption;
            DataTable dt = employeeBiz.GetListByDept(stateID, dutyID);
            int r = dt.Rows.Count;
            if (r > 0)
            {
                BindChart(r, dt, "人数", "部门名称");
              //  lbAmount.Text = "合计:    " + dt.Rows.Count;
                //chMain.Width = r*48;
                //chMain.ChartAreas["ca"].AxisX.Interval = 1;
                
                //chMain.IsSoftShadows = true;
                //chMain.DataSource = dt;
                //chMain.Series[0].YValueMembers = "人数";
                //chMain.Series[0].XValueMember = "部门名称";               
                //chMain.DataBind();

                gvList.DataSource = dt;
                gvList.DataBind();
            }
            else
            {
               // lbAmount.Text = "";
                JSUtility.Alert(this, "没有符合条件的数据");
            }
        } 
        protected void btnActiveEmp_Click(object sender, EventArgs e)
        {
             string caption = "<b>按在职统计人数</b><br />";

            gvList.Caption = caption;
            DataTable dt = employeeBiz.GetListActiveEmp();
            int r = dt.Rows.Count;
            if (r > 0)
            {
                BindChart(r, dt, "人数", "部门名称");
              //  lbAmount.Text = "合计:    " + dt.Rows.Count;
                gvList.DataSource = dt;
                gvList.DataBind();
            }
            else
            {
               // lbAmount.Text = "";
                JSUtility.Alert(this, "没有符合条件的数据");
            }
             
        }

        protected void btnBtEdu_Click(object sender, EventArgs e)
        {
            stateID = ddlStateID.SelectedValue;
            string dutyID = ddlDutyID.SelectedValue;
            string caption = "<b>按学历统计人数</b><br />";
            DataTable dt = employeeBiz.GetListByEdu(stateID, dutyID);
            int r = dt.Rows.Count;
            if (r > 0)
            {
                BindChart(r, dt, "人数", "学历");
               // lbAmount.Text = "合计:    " + dt.Rows.Count;
                gvList.Caption = caption;
                gvList.DataSource = dt;
                gvList.DataBind();
            }
            else
            {
               // lbAmount.Text = "";
                JSUtility.Alert(this, "没有符合条件的数据");
            }
        }


        protected void btnByDuty_Click(object sender, EventArgs e)
        {
            stateID = ddlStateID0.SelectedValue;
            string deptID = ddlDeptID.SelectedValue;
            string caption = "<b>按职位统计人数</b><br />";
            DataTable dt = employeeBiz.GetListByDuty(stateID, deptID);
            int r = dt.Rows.Count;
            if (r > 0)
            {
                BindChart(r, dt, "人数", "职位");
                //lbAmount.Text = "合计:    " + dt.Rows.Count;

                gvList.Caption = caption;
                gvList.DataSource = dt;
                gvList.DataBind();
            }
            else
            {
               // lbAmount.Text = "";
                JSUtility.Alert(this, "没有符合条件的数据");
            }
        }

        protected void btnByArea_Click(object sender, EventArgs e)
        {
            stateID = ddlStateID0.SelectedValue;
            string deptID = ddlDeptID.SelectedValue;
            DataTable dt = employeeBiz.GetListByArea(stateID, deptID);
            int r = dt.Rows.Count;
            if (r > 0)
            {
               // lbAmount.Text = "合计:    " + dt.Rows.Count;
                BindChart(r, dt, "人数", "区域");
                string caption = "<b>按区域统计人数 </b><br />";

                gvList.Caption = caption;
                gvList.DataSource = dt;
                gvList.DataBind();
            }
            else
            {
                //lbAmount.Text = "";
                JSUtility.Alert(this, "没有符合条件的数据");
            }
        }

        int sum = 0;
        protected void gvList_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string txt = e.Row.Cells[1].Text.Trim();
                sum += Int32.Parse(e.Row.Cells[2].Text);
                e.Row.Cells[1].Text = "<a href=javascript:ShowLink('EmpStatisticDetail.aspx?GvCaption=" + gvList.Caption.Substring(4, 2) + "&SearchParam=" + txt + "&Param1=" + ddlStateID.SelectedValue + "&Param2=" + ddlDutyID.SelectedValue + "&Param3=" + ddlStateID0.SelectedValue + "&Param4=" + ddlDeptID.SelectedValue + "',800,550)>" + txt + "</a>";
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[1].Text = "总数：";
                e.Row.Cells[2].Text = sum.ToString();
            }
        }

       

    }
}